﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Topro.Web.Controllers
{
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.Command;

    /// <summary>
    /// 台车控制器
    /// </summary>
    public class CarController : BaseController
    {
        #region 注入
        /// <summary>
        /// 指令接口
        /// </summary>
        private readonly CommandService _commandService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="commandService">指令接口</param>
        public CarController(CommandService commandService)
        {
            _commandService = commandService;
        }
        #endregion

        /// <summary>
        /// 台车管理
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<StockInQueryResponse></returns>
        public JsonResult CommandQuery(CommandQuery query)
        {
            query.OrderType = (int)OrderType.台车;

            var list = _commandService.CommandQuery(query);

            return Json(list);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View("CarAdd");
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        public JsonResult Save(CommandSaveRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                //from
                if (string.IsNullOrEmpty(request.From))
                {
                    throw new Exception("From不能为空.");
                }
                //to
                if(string.IsNullOrEmpty(request.To))
                {
                    throw new Exception("To不能为空.");
                }

                request.Ip = Ip;
                request.UserId = UserId;

                response = _commandService.Save(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
    }
}
